package top.drinyeit.nongmao.presenter;

import top.drinyeit.nongmao.activity.BalanceActivity;
import top.drinyeit.nongmao.app.Constant;
import top.drinyeit.nongmao.retrofit.HttpResponse;
import top.drinyeit.nongmao.retrofit.RetrofitMethod;
import top.drinyeit.nongmao.retrofit.subscriber.CommonSubscriber;
import top.drinyeit.nongmao.retrofit.subscriber.SubscriberListener;
import top.drinyeit.nongmao.util.SPUtil;

import static top.drinyeit.nongmao.app.Constant.COMMON_SUCCESS;

/**
 * Created by wu on 2018/1/6.
 */

public class BalancePresenter {
    private BalanceActivity activity;

    public BalancePresenter(BalanceActivity activity){
        this.activity = activity;
    }

    //获取用户余额
    public void getBalance() {
        String user_id = SPUtil.getShareString(Constant.USER_ID);
        activity.showLoading();
        CommonSubscriber<HttpResponse<Double>> subscriber = new CommonSubscriber<>(new SubscriberListener<HttpResponse<Double>>() {
            @Override
            public void onError(String e, int code) {
                activity.dismissLoading();
                activity.showToastMsg("错误，请重新操作！");
            }

            @Override
            public void onNext(HttpResponse<Double> o) {
                activity.dismissLoading();
                if (o.getCode() == COMMON_SUCCESS) {
                    activity.balance.setText("¥" + o.getData());
                    activity.balanceNumber = o.getData();
                }else {
                    activity.showToastMsg("获取余额失败,请重新获取");
                }
            }
        });
        RetrofitMethod.getInstance().getBalance(subscriber, user_id);
    }
}
